<?php
require_once('Registry.php');
require_once('Conexao.php');

class User {
    public $nome;
    public $login;
    public $senha;
    public $accessLevel;
    
    function __construct() {
    }
    function get($atributo) {
        return $this->$atributo;
    }
    function set($atributo, $valor) {
        $this->$atributo = $valor;
    }
    function logar() {
        $sql = "SELECT level
                    FROM user
                    WHERE login=:login
                    AND senha=:senha";

        $registry = Registry::getInstance();
        $conexao = $registry->get( 'PDO' );
        $consulta=$conexao->prepare($sql);
        $consulta->bindValue(":login", $this->login);
        $consulta->bindValue(":senha", $this->senha);
        $consulta->execute();
        $linha=$consulta->fetch(PDO::FETCH_ASSOC);
        if($linha){
            $return = $linha["level"];
        }else{
            $return = 0;
        }
        return $return;
    }    
    function userListar(){
        $sql = "SELECT nome, login, level
                    FROM user";
        
        $registry = Registry::getInstance();
        $conexao = $registry->get( 'PDO' );
        $consulta=$conexao->prepare($sql);
        $consulta->execute();
        $linha=$consulta->fetchAll(PDO::FETCH_ASSOC);
        return $linha;
    }
    function userBuscar() {
        $sql = "SELECT nome, login, level
                    FROM user
                    WHERE login=:login";

        $registry = Registry::getInstance();
        $conexao = $registry->get( 'PDO' );
        $consulta=$conexao->prepare($sql);
        $consulta->bindValue(":login", $this->login);
        $consulta->execute();
        $linha=$consulta->fetch(PDO::FETCH_ASSOC);
        return $linha;
    }
    function userAutocomplete($param){
            $sql = "SELECT nome, login, level
                    FROM user        
                    WHERE login LIKE :login
                    OR nome LIKE :nome";
        $registry = Registry::getInstance();
        $conexao = $registry->get( 'PDO' );
        $consulta=$conexao->prepare($sql);
        $consulta->bindValue(":login", $param.'%');
        $consulta->bindValue(":nome", $param.'%');        
        $consulta->execute();
        $linha=$consulta->fetchAll(PDO::FETCH_ASSOC);
        return $linha;
    }  
    function userCadastrar() {
        $sql = "INSERT INTO user
                    VALUES(:nome, :login, :senha, :accessLevel)";
        
        $registry = Registry::getInstance();
        $conexao = $registry->get( 'PDO' );
        $consulta=$conexao->prepare($sql);
        $consulta->bindValue(":nome", $this->nome);
        $consulta->bindValue(":login", $this->login);
        $consulta->bindValue(":senha", $this->senha);
        $consulta->bindValue(":accessLevel", $this->accessLevel);        
        $consulta->execute();
        $result = 0;
        if($consulta->rowCount()==1){
            $result=1;
        }
        return $result;
    }    
    function userExcluir() {
        $sql = "DELETE FROM user
                    WHERE login=:login";

        $registry = Registry::getInstance();
        $conexao = $registry->get( 'PDO' );
        $consulta=$conexao->prepare($sql);
        $consulta->bindValue(":login", $this->login);
        $consulta->execute();
        $result=0;
        if ($consulta->rowCount() == 1) {
            $result=1;
        }
        return $result;    
    }     
    function userAlterar() {
        $sql = "UPDATE user
                    SET nome=:nome, level=:level	
                    WHERE login=:login";

        $registry = Registry::getInstance();
        $conexao = $registry->get( 'PDO' );
        $consulta=$conexao->prepare($sql);
        $consulta->bindValue(":login", $this->login);
        $consulta->bindValue(":nome", $this->nome);
        $consulta->bindValue(":level", $this->accessLevel);        
        $consulta->execute();
        if ($consulta->rowCount() == 1) {
            $result = 1;
        } else {
            $result = 0;
        }
        return $result;
    }

    function alteraSenha() {
        $sql = "UPDATE user
                    SET senha=:senha	
                    WHERE login=:login";

        $registry = Registry::getInstance();
        $conexao = $registry->get( 'PDO' );
        $consulta=$conexao->prepare($sql);
        $consulta->bindValue(":login", $this->login);
        $consulta->bindValue(":senha", $this->senha);
        $consulta->execute();
        if ($consulta->rowCount() == 1) {
            $result = 1;
        } else {
            $result = 0;
        }
        return $result;
    }
    function escrever(){
        $file = fopen('/etc/squid/access_level/level'.$this->accessLevel, 'a');
        if($file){
            fwrite($file, $this->login."\r\n");
            fclose($file);
            return true;
        }else{
            return false;
        }
    }
    function escrever2(){
        $sql = "SELECT login FROM user
                    WHERE level=:accessLevel";

        $file = fopen('/etc/squid/access_level/level'.$this->accessLevel, 'w');
        if($file){
            $registry = Registry::getInstance();
            $conexao = $registry->get( 'PDO' );
            $consulta=$conexao->prepare($sql);
            $consulta->bindValue(":accessLevel", $this->accessLevel);
            $consulta->execute();
            $linha=$consulta->fetchAll(PDO::FETCH_ASSOC);
            foreach ($linha as $escreve){
                fwrite($file, $escreve["login"]."\r\n");                                        
            }
            fclose($file);
            return true;
        }else{
            return false;
        }
    }    
}
?>